//
//  ViewController.m
//  LianXi7.11
//
//  Created by apple on 2017/7/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImage *image;

@property (nonatomic, copy) NSString *hehe;

@property (nonatomic, copy) NSString *hehePath;

@property (nonatomic, strong) UIImage *imageC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //    NSString *str = @"http://img2.imgtn.bdimg.com/it/u=49292017,22064401&fm=26&gp=0.jpg";
    NSString *str = @"http://img06.tooopen.com/images/20170514/tooopen_sy_210122159348.jpg";
    UIImageView *ima = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //    ima.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]] scale:1];
    
    [self.view addSubview:ima];
    
    __block typeof(self) weakSelf = self;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //        self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]] scale:1];
        //        NSLog(@"__%@",_image);
        
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[NSURL URLWithString:str] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            NSString *dou = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
            
            weakSelf.hehePath = [dou stringByAppendingPathComponent:@"123.jpg"];
            
            
            [[NSFileManager defaultManager] moveItemAtPath:location.path toPath:weakSelf.hehePath error:nil];
            
                        NSLog(@"--== %@",weakSelf.imageC);
        }];
        
        [task resume];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
        
    });
    
    [self createBtnBack];
    
}

- (void)createBtnBack{
    
    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(30, 440, 50, 50)];
    
    [btnBack setTitle:@"BACK" forState:UIControlStateNormal];
    [btnBack setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    btnBack.backgroundColor = [UIColor greenColor];
    
    [btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnBack];
    
}

- (void)btnBack{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
